/*
 * Copyright (C) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "HiSpark_WiFi_IoT_SSL_VER.h"

osMutexId_t g_mutexId;

extern void nfc_network_task(const void *arg);
extern int deal_command_task(void);
extern void refresh_led();
extern int report_message_task(void);
extern void TimerTask(void *arg);

/**************************************************
 * 任务：main_task
 * 主任务
 * ***********************************************/
static int main_task(void)
{
    osThreadAttr_t attr;

    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024*5;

    /**************************************************
    * 任务1：NFC碰一碰配网
    * ***********************************************/
    attr.name = "nfc_network_task";
    attr.priority = osPriorityNormal;
    if (osThreadNew((osThreadFunc_t)nfc_network_task, NULL, &attr) == NULL) 
    {
        printf("Falied to create nfc_network_task!\n");
    }

    /**************************************************
    * 任务2：接收APP消息
    * ***********************************************/
    attr.name = "deal_command_task";
    attr.priority = osPriorityNormal;
    if (osThreadNew((osThreadFunc_t)deal_command_task, NULL, &attr) == NULL) 
    {
        printf("Falied to create deal_command_task!\n");
    }

    /**************************************************
    * 任务3：软件定时器
    * ***********************************************/
    attr.name = "TimerTask";
    attr.priority = osPriorityNormal;
    if (osThreadNew((osThreadFunc_t)TimerTask, NULL, &attr) == NULL) 
    {
        printf("Falied to create deal_command_task!\n");
    }

        /**************************************************
    * 任务4：刷新LED
    * ***********************************************/
    attr.name = "refresh_led";
    attr.priority = osPriorityNormal;
    if (osThreadNew((osThreadFunc_t)refresh_led, NULL, &attr) == NULL) 
    {
        printf("Falied to create deal_command_task!\n");
    }

    /**************************************************
    * 任务4：上报设备数据
    * ***********************************************/
    attr.name = "report_message_task";
    attr.priority = 27;
    if (osThreadNew((osThreadFunc_t)report_message_task, NULL, &attr) == NULL)
    {
        printf("Falied to create report_message_task!\n");
    }

    g_mutexId = osMutexNew(NULL);
    if (g_mutexId == NULL) {
        printf("Failed to create Mutex!\n");
    }

}

/**************************************************
 * 任务：main_entry
 * 主任务，创建几个任务，如创建初始化任务、平台任务、传感器任务
 * ***********************************************/
static void main_entry(void)
{
    IoT_SSL_VER_Init();
    osThreadAttr_t attr;

    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024*10;
    attr.name = "iot_main";
    attr.priority = osPriorityNormal;
    if (osThreadNew((osThreadFunc_t)main_task, NULL, &attr) == NULL)
    {
        printf("Falied to create main_task!\n");
    }
}

APP_FEATURE_INIT(main_entry);